export class PageDataModel<T> {
  total: number;
  pageSize: number;
  currentPage: number;
  lastPage: number;
  data: T[];

  // 数据未加载
  get isCompleted(): boolean {
    return this.currentPage > 0;
  }

  // 当前页是否为空
  get isEmpty(): boolean {
    return this.currentPage > 0 && this.data.length === 0;
  }

  // 数据长度
  get length(): number {
    return this.data.length;
  }

  // 当前页是否是最后一页
  get isLastPage(): boolean {
    return this.currentPage >= this.lastPage;
  }

  // 下一页
  get nextPage(): number {
    return this.currentPage + 1;
  }

  // 构造空对象
  constructor(perPage: number = 15) {
    this.total = 0;
    this.currentPage = 0;
    this.lastPage = 1;
    this.pageSize = perPage;
    this.data = [];
  }

  // 合并页面数据
  merge(page: PageDataModel<T>): void {
    this.total = page.total;
    this.pageSize = page.pageSize;
    this.currentPage = page.currentPage;
    this.lastPage = page.lastPage;
    this.data = [...this.data, ...page.data];
  }

  // 清空数据
  clear(): void {
    this.total = 0;
    this.currentPage = 0;
    this.lastPage = 1;
    this.pageSize = 15;
    this.data = [];
  }

  // 重置数据
  reset(): void {
    this.clear();
  }

  // copyWith
  copyWith(data: PageDataModel<T>): PageDataModel<T> {
    const instance = new PageDataModel<T>(data.pageSize);
    instance.total = data.total ?? this.total;
    instance.currentPage = data.currentPage ?? this.currentPage;
    instance.lastPage = data.lastPage ?? this.lastPage;
    instance.data = data.data ?? this.data;
    return instance;
  }

  // 静态方法，生成空的分页数据
  static empty<T>(perPage: number = 15): PageDataModel<T> {
    const instance = new PageDataModel<T>(perPage);
    instance.total = 0;
    instance.currentPage = 0;
    instance.lastPage = 1;
    instance.data = [];
    return instance;
  }
}
